package cn.thoughtworks.school.repositories;

import cn.thoughtworks.school.entities.Notification;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface NotificationRepository extends JpaRepository<Notification, Long> {

  List<Notification> findAllByIsReadAndReceiverIdOrderByIdDesc(Boolean isRead, Long userId);

  @Modifying
  @Transactional
  @Query("update Notification n set isRead=?2 where n.receiverId=?1")
  void updateByUserIdAndStatus(Long userId, Boolean status);

  Page<Notification> findAllByIsReadAndReceiverId(Boolean isRead, Long userId, Pageable pageable);
}
